﻿using BaiTu.EventEntity;
using BaiTu.EventServices.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.EventServices
{
    internal static class EventProcessorHandlerSearcher
    {
        static List<string> listMappingType = new List<string>();
        /// <summary>
        /// 根据事件类Type返回相应的处理器Type
        /// </summary>
        /// <param name="eventClassType"></param>
        /// <returns></returns>
        public static EventInfo Find(string eventClassType)
        {


            var result = from tb in EventServiceConfiguration.EventMappings
                         where tb.ProcessClassType.Equals(eventClassType)
                         select tb;

            EventInfo em = result.FirstOrDefault();

            //EventProcessorInfo info = new EventProcessorInfo();
            //info.EventProcessorAssemblyName = em.ProcessAssemblyName;
            //info.EventProcessorClassType = em.ProcessClassType;
            return em;
        }
        private static bool CheckMappingExists(string classType)
        {
            if (EasyCheckMapping(classType))
            {
                return true;
            }
            bool flag = false;
            EventInfo eventInfo = new EventInfo()
            {
                SourceClassType = classType
            };
            foreach (var item in EventServiceConfiguration.EventMappings)
            {
                if (item.SourceClassType.Equals(classType))
                {
                    listMappingType.Add(classType);
                    flag = true;
                    break;
                }
            }
            return flag;
        }
        private static bool EasyCheckMapping(string classType)
        {
            return listMappingType.Contains(classType);
        }
    }
}
